package com.it.zhangjun.arithmetic.链表;

/**
 * @author zhangj
 * @description 删除链表中的节点
 * @date 2021-03-17 17:08
 */
public class LT_237 {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        root = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        deleteNode1(node2);
        System.out.println(root);
    }

    private static ListNode root;

    public static void deleteNode1(ListNode delNode) {
        ListNode node = root;
        ListNode prev = null;
        while (node != null) {
            if (delNode.val == node.val) {
                break;
            }
            prev = node;
            node = node.next;
        }
        if (prev == null) {
            root = node.next;
        } else {
            prev.next = node.next;
        }
    }

    public static void deleteNode2(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }

}